home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!eskimo!scs
- From: scs@eskimo.com (Steve Summit)
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- X-Nntp-Posting-Host: eskimo.com
- Message-ID: <DnLKHM.3wB@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: schmorganization
- References: <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
- Date: Fri, 1 Mar 1996 16:29:46 GMT
-
- Brenda, please fix your newsposting software so that your From:
- line is usable so that we can reply to you by e-mail rather than
- hashing this all out in the newsgroup. The articles you've been
- posting contain the line
-
- From: Brenda <g051286>
-
- which is not something I can reply to; it needs to say something
- like
-
- From: Brenda <g051286@gte.com>
- or
- From: g051286@gte.com (Brenda)
-
- (If you're using Netscape, go into the Network/Mail Preferences
- setup, and set your e-mail address to a complete Internet e-mail
- address, including the @ sign. Also, complain to Netscape and
- GTE that they allowed you to look foolish by posting articles to
- which no one could reply.)
-
- In article <4h1u9d$sqq@ceylon.gte.com>, Brenda <g051286> writes:
- > What is the definition of a pointer? I have always been taught that a
- > pointer is simply an address in memory, and an array name (i.e. myarray)
- > is simply a CONSTANT address.
-
- These are fuzzy definitions. Slightly more precise would be to
- say that a pointer *value* is an address, that a pointer
- *variable* *holds* an address, and that an array *has* a constant
- address.
-
- Informal definitions (such as the "fuzzy" ones you gave, and the
- "slightly more precise" ones I just gave) are fine in some
- contexts. In particular, as I argue in another thread spun off
- of this one, it's perfectly reasonable to think of a pointer, as
- used in certain contexts, as a kind of "array" (the context
- being, of course, when the pointer points to a contiguous block
- of elements which you're indexing into). But it is not useful to
- think of an array as a pointer. An array is not a pointer, and
- it doesn't even act like one. (All of this confusion arises
- because pointers are automatically generated when you use arrays
- in most expressions.)
-
- The other problem with fuzzy definitions is that they tend to
- get you into trouble out here in this nonstop flamefest called
- Usenet, where some of us (too many of us?) are incorrigible
- nitpickers and language lawyers who use ridiculously precise
- definitions and bristle at inaccurate ones.
-
- > Of course there are differences between arrays and pointers
- > due to the fact that an array is a CONSTANT address and a pointer is a
- > VARIABLE address.
-
- The differences involve much more than that. When I say
-
- int a, b, *ip;
- if(some_condition)
- ip = &a;
- else ip = &b;
-
- I'm doing something with a pointer which I can never do with an
- array, because what I'm doing doesn't even act like an array.
- When I say
-
- char line[100];
- fgets(line, (int)sizeof(line), stdin);
-
- I'm doing something with an array which I can never do (in quite
- that way) with a pointer, because the compiler automatically
- allocates space for arrays and (usually) knows how big they are.
-
- > And the reason I said you shouldn't (note shouldn't
- > not couldn't) say &myarray is:
- > ==================================================
- > (from "A Book On C", Kelley & Pohl, pg 200)
- > Constructs not to be pointed at:
- > 1. Do not point at constants. (&3)
- > 2. Do not point at arrays; an array name is a constant. (int a[77]; &a)
- > 3. Do not point at ordinary expressions &(k + 99)
- > 4. Do not point at register variables. (register v; &v)
- > The address operator can be applied to variables and array elements.
- > ===================================================
-
- Okay, I do note that you said "shouldn't" and not "couldn't".
- I agree that, the vast majority of the time, you shouldn't write
- &a where a is an array, because it usually isn't what you want.
- Situations 1, 3, and 4, however, are places where you shouldn't
- because you *can't*. Kelley & Pohl are being a little imprecise
- here, too.
-
- > So again I say, myarray is DEFINITELY a pointer (i.e. address in memory).
-
- We keep trying to tell you, but you just won't listen.
- myarray *has* an address in memory, just as all variables
- (simple, pointer, or array) do. If I say
-
- int i, *ip, myarray[10];
-
- then i, ip, and myarray all have addresses. Only ip is a
- pointer, and besides the address that it *has*, there is another
- address that it can *hold*: the address of the thing that it
- points to. myarray, on the other hand, is in and of itself in no
- way shape or form a pointer; it is an array, and that is why we
- call it an array. Due to the "equivalence" of arrays and
- pointers in C, when we *use* myarray in most expressions, the
- compiler will generate a pointer to its first element. That
- doesn't mean that myarray is a pointer, any more than a saw is a
- birdhouse because I can use it to make one, or a camera is a
- picture because I can use it to take one.
-
- Finally, be careful with that word "address." I like to call
- unary & the "pointer to" operator, not the "address of" operator.
- If you're not an assembly language programmer (and the whole
- point of using an allegedly higher level language like C is to
- avoid having to program in assembler), it is nearly impossible to
- think or even speak precisely about the distinction between the
- address *of* a pointer variable and the address which the pointer
- variable *contains*. If instead you think of all variables
- (simple, array, and pointer) as locations in memory that can hold
- values, and that the value of a pointer variable is a pointer to
- another location, you won't fall into the trap of thinking that
- an array is what a pointer has.
-
- Steve Summit
- scs@eskimo.com
- --
- The Communications Decency Act within the Telecommunications Act
- of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
- and obscene piece of legislation which attempts to ban annoying,
- threatening, abusive, indecent, or obscene communication.
-